import uuid from 'uuid'
import storage from './storage'
import configMes from './configMes'
import system from './system'
import _ from 'lodash'


const _trackObj = {
    houseid: '109',
    uid: '',
    phone: '',
    time: 0, //
    pageid: '', //
    system: '3',
    product: '109',
    version: '1',
    project: '2',
    type: '20',
    keyvalue: '',
    sessionId:'',
    module:'3'
}

const _key = 'sky-forest-ubt';

let setIntervalKey = 0;

function _commit(data) {
    var xhr = new XMLHttpRequest();
   // console.log("url:"+configMes.getSkyForestUrl('/skyforest/analysis/add'));
    xhr.open('post', configMes.getSkyForestUrl('/skyforest/analysis/add'), true);
    xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
    xhr.send(JSON.stringify(data));
  //  console.log("sendEnd Data:"+JSON.stringify(data));
}

class Ubt {
    constructor() {
        _trackObj.uid = this.getUid();
        _trackObj.phone = configMes.getPhone() || window.mobile || '';
        _trackObj.sessionId = configMes.getSessionid() || window.sessionid ||'';
        _trackObj.version = configMes.getVersion() || window.myVersion || '1';
    }
    reset(){
        _trackObj.uid = this.getUid();

        _trackObj.phone =  system.getMobile() ||window.mobile|| '';
        _trackObj.sessionId = configMes.getSessionid() || window.sessionid ||'';
        _trackObj.version = configMes.getVersion() || window.myVersion || '1';
    }
    getUid() {
        let uid = storage.get(_key, 'uuid');
        if(window.uuid){

        //    console.log("window.uuid:"+window.uuid);
            storage.set(_key, 'uuid', window.uuid);
            return window.uuid;
        }

        if (uid) {
       //     console.log("old:"+uid);
            return uid;
        }


        // uid = uuid.v1();
        // storage.set(_key, 'uuid', uid);
        // console.log("new:"+uid);
        // return uid;
    }

    trackPageview(pageid, key,parent_pageid, id='') {
        this.reset();
        const data = _.clone(_trackObj);
        if(sessionStorage.getItem('imhouseid')){
            data.houseid = sessionStorage.getItem('imhouseid');
            data.product = sessionStorage.getItem('imhouseid');
        }
        data.pageid = pageid;
        data.time = new Date().valueOf();
        data.keyvalue = key;
        data.resourceId = id;
        data.resourceName = '';
        data.parent_pageid = parent_pageid;
        data.url = location.href;
        if(system.isIosApp()){
            data.module = '1';
        }else if(system.isAndroidApp()){
            data.module = '2';
        }else{
            data.module = '3';
        }

        _commit(data);
    }

    trackEvent(pageid,key,resourceId,resourceName,category=null,eventType='click',label=null,value=null,attrs=null,photoname=null,categoryname=null,name=null,commenttitle=null,phonenum=null,videoname=null,parent_pageid=null,plancode = null,usetime = null,url=null,spacename=null,sellname=null,title=null,click_sortno=null){
        this.reset();
        const data = _.clone(_trackObj);
        if(sessionStorage.getItem('imhouseid')){
            data.houseid = sessionStorage.getItem('imhouseid');
            data.product = sessionStorage.getItem('imhouseid');
        }
        data.pageid = pageid;
        data.time = new Date().valueOf();
        data.keyvalue = key||'';
        data.resourceId = resourceId||'';
        data.resourceName = resourceName||'';
        data.typeEvent = eventType||'';
        data.category = category||'';
        data.label = label||'';
        if(value == 0){
            data.value = 0;
        }else{
            data.value = value||'';
        }

        data.photoname = photoname||'';
        data.categoryname = categoryname||'';
        data.name = name||'';
        data.commenttitle = commenttitle||'';
        data.phonenum = phonenum||'';
        data.videoname =videoname||'';
        data.parent_pageid = parent_pageid||'';
        data.plancode = plancode||'';
        data.usetime = usetime||'';
        data.url = url||location.href||'';
        data.spacename = spacename||'';
        data.sellname = sellname||'';
        data.title = title||'';
        data.click_sortno = click_sortno||'';
        if(system.isIosApp()){
            data.module = '1';
        }else if(system.isAndroidApp()){
            data.module = '2';
        }else{
            data.module = '3';
        }

        if(attrs){
            data.attrs = JSON.stringify(attrs);
        }
     //   console.log("sessionid:ubt:"+data.sessionId);
        _commit(data);
    }

    trackUserTime(pageid, key,resourceId,resourceName,videoname=null,) {
        const duration = 3000;
        const that = this;

        clearInterval(setIntervalKey);

        if(pageid&&key){
            setIntervalKey = setInterval(function(){
                if(pageid=='01011092'){
                    that.trackEvent(pageid,key, resourceId,resourceName,JSON.parse(sessionStorage.getItem('adviserInfo')).id,'','','0','','','',JSON.parse(sessionStorage.getItem('adviserInfo')).tel)
                }
                else{
                    that.trackEvent(pageid,key,resourceId,resourceName,'','statetime',null,'','','','','','','','','','',duration);
                }

            },duration)
        }
    }
}

export default new Ubt();

